//
//  HotTableViewController.m
//  rubaoduanzi
//
//  Created by 郝志华 on 16/9/13.
//  Copyright © 2016年 郝志华. All rights reserved.
//

#import "HotTableViewController.h"
#import <AFNetworking.h>
#import "FrameModel.h"
#import "HotModel.h"
#import "HotTableViewCell.h"
#import <MJRefresh.h>
@interface HotTableViewController ()
@property(nonatomic,strong)NSMutableArray *arrM;


@end

@implementation HotTableViewController
- (NSMutableArray *)arrM{
    if (!_arrM) {
        _arrM=[NSMutableArray array];
    }
    return _arrM;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.tableView.backgroundColor=[UIColor whiteColor];
    [self loadData];
    


    // Uncomment the following line to preserve selection between presentations.
    // self.clearsSelectionOnViewWillAppear = NO;
    
    // Uncomment the following line to display an Edit button in the navigation bar for this view controller.
    // self.navigationItem.rightBarButtonItem = self.editButtonItem;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {

    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

    return self.arrM.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString *ID=@"cell";
    FrameModel *fram=self.arrM[indexPath.row];

    HotTableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:ID];
    if (cell==nil) {
        cell=[[HotTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:ID];
    }
    cell.frameModel=fram;
    return cell;
}
- (void)loadData{
    AFHTTPSessionManager *manager=[AFHTTPSessionManager manager];
    [manager GET:@"http://mobileapi1.roboo.com/joke/hotJson.do?p=1&ps=14&sort=yes&noimg=false&acc=" parameters:nil progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        if (responseObject!=nil) {
            NSLog(@"成功！");
            NSDictionary *dict=responseObject;
            NSArray *arr=dict[@"items"];
            for (NSDictionary *dic in arr) {
                HotModel *model=[HotModel hotModelWithDict:dic];
                FrameModel *frame=[[FrameModel alloc]init];
                frame.model=model;
                [self.arrM addObject:frame];
            }
            [self.tableView reloadData];
            NSLog(@"%ld",self.arrM.count);
        }

    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        NSLog(@"%@",error);
    }];

}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    FrameModel *model=self.arrM[indexPath.row];
    return model.rowHeight;

}
@end
